﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using Tools;

    public class AddGameResourceEditor : ScriptableWizard
    {


        static GameResource manager = null;

        static List<Sprite> res = new List<Sprite>();

        static List<AudioClip> audios = new List<AudioClip>();


        static bool Init()
        {

            if (manager == null)
            {
                manager = GameObject.FindObjectOfType<GameResource>();

                if (manager == null)
                {
                    Debug.LogError("no find GameResource!");
                    return false;
                }
            }

            return true;
        }


    [MenuItem("EditorTool/Add Game Resource/Sprites ")]
    static void addSprites()
    {

        if (Init())
        {
            loadSprites();

            for (int i = 0; i < res.Count; i++)
            {
                Sprite s = res[i];

                if (manager.AllSprites.Contains(s))
                {
                    Debug.Log("已经包含资源:" + s.name);
                    continue;
                }
                manager.AllSprites.Add(s);
            }

            EditorUtility.SetDirty(manager);
        }
    }




    [MenuItem("EditorTool/Add Game Resource/AudioClips ")]
    static void addAudios()
    {
     
        if (Init())
        {
            LoadAudios();

            for (int i = 0; i < audios.Count; i++)
            {
                AudioClip s = audios[i];

                if (manager.AllMusic.Contains(s))
                {
                    Debug.Log("已经包含资源:" + s.name);
                    continue;
                }
                manager.AllMusic.Add(s);
            }

            EditorUtility.SetDirty(manager);
        }
    }








    static void loadSprites()
        {
            res.Clear();
            Object[] selects = Selection.objects;

            if (selects == null || selects.Length <= 0)
            {
                Debug.Log("没有选择相应的图片资源");
                return;
            }

            for (int i = 0; i < selects.Length; i++)
            {
                Object sel = selects[i];

                Sprite s = sel as Sprite;

                if (s == null)
                {
                    Debug.Log("选择的资源不是sprite:" + sel.name);
                    continue;
                }
                res.Add(s);
            }

        }





        static void LoadAudios()
        {
            audios.Clear();

            Object[] selects = Selection.objects;

            if (selects == null || selects.Length <= 0)
            {
                Debug.Log("没有选择相应的音乐资源");
                return;
            }

            for (int i = 0; i < selects.Length; i++)
            {
                Object sel = selects[i];

                AudioClip s = sel as AudioClip;

                if (s == null)
                {
                    Debug.Log("选择的资源不是AudioClip:" + sel.name);
                    continue;
                }
                audios.Add(s);
            }
        }


    }



